package smsBean;

import java.nio.Buffer;
import java.sql.ResultSet;

import java.util.Hashtable;
import java.util.Vector;

import control.Command;

import utility.CharAt;
import utility.ConvertToLower;
import utility.Index;
import utility.MessageGenerator;


public class SmsRate_master extends SubSmsBean
{

	private String tableName = "rate_master";
	
	public Hashtable<Object,Object> RateMap = new Hashtable<Object,Object>();
	Vector<Object> RateList =  new Vector<Object>(); /** Vector for storing number of rate_master object for multiple properties.*/
	
	private Hashtable<Object,Object> ViewHashtable = new Hashtable<Object, Object>();
	
	
	public SmsRate_master() {
		// TODO Auto-generated constructor stub
	}
	
	public Hashtable<Object,Object> initializeToAdd()
	{
	
		this.sqlDB=super.sqlDB;
	
		RateMap.putAll(initRate_master());	
		
		if(RateMap.containsKey("ErrorInput"))
				return RateMap;	
		return RateMap;	
	}
	
	Hashtable<Object,Object> initRate_master()
	{
		int i = 0;	
		String msg = message;
		String SPchar=null;		
		int count=0; /** Stores the total number of special characters in Command message given by User.*/
		ResultSet rs = null; 
		
		//GET THE SPECIAL CHARACTER FOR THE PROPERTY DETAILS;
		try
		{
			rs = sqlDB.getSelect("select fieldname,spchar from command where msgtype = '" + tableName + "' and process = '"+process+"' and spchar is not null");	//Add Process
			rs.next();
			SPchar = rs.getString("spchar");
			rs.close();	
			
			
			
			while(i<msg.length())
			{			
				char ch = CharAt.CharAT(msg,i);
				if(ch == SPchar.toCharArray()[0])
					count ++;
				i++;
			}
			
			System.out.println("pool::Rate_master::initRate_master():: Special Char : "+SPchar);
			System.out.println("pool::Rate_master::initRate_master():: Count : "+count);		
		}
		catch (Exception e) 
		{
			System.out.println("pool::Rate_master::initRate_master():: Exception::");
			e.printStackTrace();
		}
		
		try
		{
			int cnt=0;
			
			while(cnt!=count)
			{				
				RateMap.putAll(property(message, cnt+1));
				RateList.add(RateMap);
				cnt++;
			}
		}
		catch(Exception e)
		{
			System.out.println("pool::Rate_master::initRate_master():: Exception::");
			e.printStackTrace();
		}
		return RateMap;
	}
	
	
	public Hashtable<Object,Object> property(String str, int cnt)
	{
		int count=0;
		String spChar = null;
		String tempStr=str;
		int i=0, start=0,end=0;
		String retStr=null;
		
		Command cd=new Command(tableName,sqlDB);
		Hashtable<Object,Object> newRateMap=new Hashtable<Object,Object>();
		
		char cha=CharAt.CharAT(tempStr,0);
		spChar=""+cha;
		while(i<tempStr.length())
		{
			char ch = CharAt.CharAT(tempStr,i);					
			if(ch==cha)
			{
				count ++;
				
				if (count == cnt)
				{		
					start = Index.indexOf(str, spChar,i);
					end = Index.indexOf(str, spChar,start+1);
					
					if (end == -1)
					{
						retStr = str.substring(start,message.length());								
					}
					else
						retStr = str.substring(start,end);
					
					System.out.println("String needed -->> " + retStr);
					newRateMap = ConvertToLower.convertHashKey(cd.getHashCommand(tableName, retStr,process));
					
					if(newRateMap.containsKey("ErrorInput"))
					{
						return newRateMap;
					}
					
					return newRateMap;
					
					
				}
			}
			i++;
		}
	return newRateMap;
	
	}
	
	public StringBuffer initializeToUpdate() {
		// TODO Auto-generated method stub
		return null;
	}

	public StringBuffer initializeToView()
	{
		StringBuffer sbrProperty=new StringBuffer();
		
		Command cp = new Command(tableName, sqlDB);
		RateMap = cp.getHashCommand(tableName,message,process); 
		if(RateMap.containsKey("ErrorInput"))
		{
			sbrProperty.append(RateMap.get("ErrorInput"));
			return sbrProperty;
		}		
		
		ViewHashtable.putAll(RateMap);
		
		
			return sbrProperty;
	
	}
	
	public Vector<Object> getPropertyVector() {
		// TODO Auto-generated method stub
		return RateList;
	}

	public Hashtable<Object, Object> getViewHashtable() {
		// TODO Auto-generated method stub
		return ViewHashtable;
	}

	
	public Hashtable<Object, Object> initializeToDelete() {
		// TODO Auto-generated method stub
		return null;
	}
	public StringBuffer generatesms(StringBuffer strbuffer)
	{	
		StringBuffer result = new StringBuffer();
		String heading[] = strbuffer.toString().split("\n")[0].split("\t");
		String splitbuffer[] = strbuffer.toString().split("\n");
		MessageGenerator mg = new MessageGenerator(sqlDB);
		for(int i=0;i<splitbuffer.length;i++)
		{
			String split[] = splitbuffer[i].split("\t");
			Hashtable<Object, Object> table = new Hashtable<Object, Object>();
			try {
				for(int j=0;j<heading.length;j++)
				{
					table.put(heading[j],split[j]);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			result.append(mg.MasterMessage(tableName, table,"view" )+"#");
		}
		return result;
	}

}
